package com.fms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fms.dto.MenuComponent;
import com.fms.dto.MenuDto;
import com.fms.entity.Menu;
import com.fms.entity.MenuCheck;
import com.fms.mapper.MenuCheckMapper;
import com.fms.mapper.MenuMapper;
import com.fms.service.MenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    private final MenuMapper menuMapper;

    private final MenuCheckMapper menuCheckMapper;
    @Override
    //获取所有菜单列表
    public List<Menu> getMenus() {
        LambdaQueryWrapper<Menu> query = new LambdaQueryWrapper<>();
        query.ne(Menu::getStatus,0);
        return menuMapper.selectList(query);
    }

    @Override
    //获取部门菜单内容
    public List<MenuDto> getMenuOption(Long id) {
        //获取菜单对应权限
        LambdaQueryWrapper<MenuCheck> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MenuCheck::getDeptId, id);
        List<MenuCheck> menuChecks = menuCheckMapper.selectList(queryWrapper);

        LambdaQueryWrapper<Menu> query = new LambdaQueryWrapper<>();
        List<Menu> menus = menuMapper.selectList(query);
        //组装前端菜单数据
        List<MenuDto> result = new ArrayList<>();
        for (Menu menu : menus) {
            //一级菜单
            if(menu.getStatus() == 0){
                MenuDto dto = new MenuDto();
                dto.setLabel(menu.getMenuName());
                List<MenuComponent> list = new ArrayList<>();
                //二级菜单
                for (MenuCheck check : menuChecks) {
                    for (Menu menu1 : menus){
                        if (check.getMenuId().equals(menu1.getId()) && menu1.getStatus().equals(menu.getId())){
                            MenuComponent component = new MenuComponent();
                            component.setLabel(menu1.getMenuName());
                            component.setIcon(menu1.getIcon());
                            component.setTo(menu1.getToPath());
                            list.add(component);
                        }
                    }
                }
                dto.setItems(list);
                result.add(dto);
            }
        }
        return result;
    }
}
